
const receiveJson = {
    "5220": 8,
    "5221": 0,
    "5222": 4,
    "5223": 4,
    "5224": 4,
    "5225": 4,
    "5226": 4,
    "5227": 4,
    "5228": 4,
    "5229": 8,
    "522A": 0,
    "522B": 4
};


const settingJSON = {
    "5200": 4,  // 转子
    "5201": 4,  // 转速
    "5202": 4,  // 型号
    "5203": 4,  // 采样间隔
    "5204": 4  // 采样时间
};


const settingArray = ["5200", "5201", "5202", "5203", "5204"];

let data = "5aa50582630000015aa50b8252200000000100020005";
let dataArray = extractData(data);
console.info(dataArray)



function extractData(values) {
    // 分片数据的处理
    console.info(values)
    values = values.toUpperCase();
    let sendHead = values.substr(0, 4);
    if(sendHead !== "5AA5"){
        return [];
    }

    let result = [];

    let splitArray = splitData(values);
    console.info(splitArray)
    if (isEmptyObject(splitArray) || splitArray.length === 0) {
        return result;
    }


    for (let i = 0; i < splitArray.length; i++) {
        let valueStr = splitArray[i];
        console.info(valueStr)

        let op = valueStr.substr(8, 4);
        // 所有数据的接受
        let length = parseInt(valueStr.substr(4,2), 16);
        let totalValueLength = length * 2;
        // length check
        if (valueStr.length !== totalValueLength + 6) {
            continue;
        }


        let dataType = valueStr.substr(6, 2);
        let dataStart = 12;
        if(dataType === "83"){
            dataStart = 14;
        }
        console.info(op)
        if(op === "6300") { //仪器状态接受数据处理
            result.push(
                {
                    "op" : op.toUpperCase(),
                    "valueLength" : totalValueLength,
                    "value" : valueStr.substr(dataStart)
                }
            );
        } else {
            let firstLength = receiveJson[op];
            if (isEmptyObject(firstLength)) {
                // 设置参数的解析
                let settingSize = settingJSON[op.toUpperCase()];
                if (isEmptyObject(settingSize)) {
                    continue;
                }

                let index = 0;
                for (; index < settingArray.length; index++) {
                    if (settingArray[index] === op.toUpperCase()) {
                        break;
                    }
                }

                // 按照settingArray顺序解析
                let start = 0;
                let curValueLength = valueStr.length - dataStart;
                while (start < curValueLength && index < settingArray.length) {
                    let size = settingJSON[op];
                    if(isEmptyObject(size)){
                        size = 4;
                    }
                    if(start + size <= curValueLength){
                        result.push(
                            {
                                "op" : settingArray[index].toUpperCase(),
                                "valueLength" : size,
                                "value" : valueStr.substr(dataStart + start, size)
                            }
                        )
                    } else {
                        result.push(
                            {
                                "op" : settingArray[index].toUpperCase(),
                                "valueLength" : size,
                                "value" : valueStr.substr(dataStart + start)
                            }
                        )
                    }
                    index++;
                    start = start + size;
                }
            } else {
                // 多数据的支持
                if (firstLength >= totalValueLength){  //单个数据的返货和接口
                    result.push({
                        "op" : op,
                        "valueLength" : firstLength,
                        "value" : valueStr.substr(dataStart)
                    });
                } else {  // 批量数据的发送和接口， 没有多余的数据
                    let start = 0;
                    let opInt = parseInt(op, 16);
                    let index = 0;
                    let curValueLength = valueStr.length - dataStart;
                    while (start < curValueLength) {
                        let size = receiveJson[(opInt + index).toString(16).toUpperCase()];
                        if(isEmptyObject(size)){
                            size = 4;
                        }
                        if(start + size <= curValueLength){
                            result.push(
                                {
                                    "op" :(opInt + index).toString(16).toUpperCase(),
                                    "valueLength" : size,
                                    "value" : valueStr.substr(dataStart + start, size)
                                }
                            )
                        } else {
                            result.push(
                                {
                                    "op" : (opInt + index).toString(16).toUpperCase(),
                                    "valueLength" : size,
                                    "value" : valueStr.substr(dataStart + start)
                                }
                            )
                        }
                        index++;
                        start = start + size;
                    }
                }
            }
        }
    }

    return result;

}

function splitData(valueList) {

    let arr = valueList.split("5AA5");
    console.info(arr)
    let ans = [];
    for(let i = 1; i < arr.length; i++) {
        ans.push("5AA5" + arr[i]);
    }
    return ans;

}

function isEmptyObject(value) {
    return value === undefined || value === null;
}

